﻿using System.ComponentModel;

namespace DiffuseEnjoyIdle.Core.Helper
{
    public static class EnumHelper
    {
        /// <summary>
        /// 获取枚举value
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static string GetDescription(this Enum value)
        {
            DescriptionAttribute[] array = (DescriptionAttribute[])value.GetType().GetField(value.ToString())!.GetCustomAttributes(typeof(DescriptionAttribute), inherit: false);
            if (array.Length == 0)
            {
                return value.ToString();
            }

            return array[0].Description;
        }

        public static string GetDescription(this Type type)
        {
            DescriptionAttribute[] array = (DescriptionAttribute[])type.GetCustomAttributes(typeof(DescriptionAttribute), inherit: true);
            if (array.Length == 0)
            {
                return string.Empty;
            }

            return array[0].Description;
        }

        public static string GetCategory(this Enum value)
        {
            CategoryAttribute[] array = (CategoryAttribute[])value.GetType().GetField(value.ToString())!.GetCustomAttributes(typeof(CategoryAttribute), inherit: false);
            if (array.Length == 0)
            {
                return string.Empty;
            }

            return array[0].Category;
        }

        public static TEnum[] ToList<TEnum>() where TEnum : struct
        {
            Type typeFromHandle = typeof(TEnum);
            List<TEnum> list = new List<TEnum>();
            foreach (TEnum value in Enum.GetValues(typeFromHandle))
            {
                list.Add(value);
            }

            return list.ToArray();
        }

    }
}
